package com.iscc.fake.controller;

import com.google.gson.Gson;
import com.iscc.fake.domain.Address;
import com.iscc.fake.domain.User;
import com.iscc.fake.service.AddressService;
import com.iscc.fake.service.Impl.AddressServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet({"/add_address", "/select_address", "/del_address", "/update_address"})
public class AddressServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String uri = request.getRequestURI();//URI目标为 /fake/register.do；
        String action = uri.substring(uri.lastIndexOf("/") + 1);
        PrintWriter out = response.getWriter();
        //转化为Json
        Gson gson = new Gson();

        HttpSession session=request.getSession();
        /*User user = new User(1);

        session.setAttribute("user",user);*/


        AddressService addressService = new AddressServiceImpl();

        Map<String, Object> addressMap = new HashMap<>();

        if ("select_address".equals(action)) {
            List<Address> addressList = addressService.selectAddress();
            if (addressList.size() > 0) {
                addressMap.put("code", 200);
                addressMap.put("msg", "查询收获地址成功");
                addressMap.put("result", addressList);
            } else {
                addressMap.put("code", 201);
                addressMap.put("msg", "暂无收获地址，请添加");
            }
        } else if ("add_address".equals(action)) {
            String city = request.getParameter("city");
            String address = request.getParameter("address");
            int addphone = Integer.parseInt(request.getParameter("addphone"));
            /*String uno = request.getParameter("uno");*/

            //从session中拿到
            int uno = ((User) session.getAttribute("user")).getUno();

            String remark = request.getParameter("remark");
            String name = request.getParameter("name");
            Address address1 = new Address(city, address, addphone, uno, remark, name);
            int add = addressService.add(address1);
            if (add != -1) {
                addressMap.put("code", 200);
                addressMap.put("msg", "地址添加成功");
            } else {
                addressMap.put("code", 201);
                addressMap.put("msg", "地址添加失败");
            }

        } else if ("del_address".equals(action)) {
            int addno = Integer.parseInt(request.getParameter("addno"));
            int del = addressService.del(addno);
            if (del != -1) {
                addressMap.put("code", 200);
                addressMap.put("msg", "删除信息成功");
            } else {
                addressMap.put("code", 201);
                addressMap.put("msg", "添加信息失败");
            }
        } else if ("update_address".equals(action)) {
            int addno=Integer.parseInt(request.getParameter("addno"));
            String city = request.getParameter("city");
            String address = request.getParameter("address");
            int addphone = Integer.parseInt(request.getParameter("addphone"));
            int uno = ((User) session.getAttribute("user")).getUno();
            String remark = request.getParameter("remark");
            String name = request.getParameter("name");
            Address address2 = new Address(addno,city, address, addphone, uno, remark, name);
            int add = addressService.update(address2);
            if (add != -1) {
                addressMap.put("code", 200);
                addressMap.put("msg", "更新信息成功");
            } else {
                addressMap.put("code", 201);
                addressMap.put("msg", "更新信息失败");
            }
        }
        String str = gson.toJson(addressMap);
        out.print(str);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
